<template>
  <div class="table-box">
    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="工装编码" prop="toolingCode">
        <el-input v-model="searchForm.toolingCode" placeholder="请输入工装编码" clearable />
      </el-form-item>
      <el-form-item label="工装名称" prop="toolingName">
        <el-input v-model="searchForm.toolingName" placeholder="请输入工装名称" clearable />
      </el-form-item>
      <el-form-item label="工装类型" prop="toolingType">
        <el-select v-model="searchForm.toolingType" placeholder="请选择类型" clearable style="width: 180px">
          <el-option label="夹具" value="FIXTURE" />
          <el-option label="模具" value="MOLD" />
          <el-option label="工具" value="TOOL" />
          <el-option label="量具" value="GAUGE" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option label="可用" value="AVAILABLE" />
          <el-option label="使用中" value="IN_USE" />
          <el-option label="维修中" value="MAINTENANCE" />
          <el-option label="报废" value="SCRAPPED" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增工装夹具</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="toolingCode" label="工装编码" />
      <el-table-column prop="toolingName" label="工装名称" />
      <el-table-column prop="toolingType" label="工装类型">
        <template #default="scope">
          <el-tag :type="getTypeColor(scope.row.toolingType)">{{ getTypeText(scope.row.toolingType) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="specifications" label="规格型号" />
      <el-table-column prop="manufacturer" label="制造商" />
      <el-table-column prop="purchaseDate" label="采购日期" />
      <el-table-column prop="lifespan" label="使用寿命" />
      <el-table-column prop="currentUsage" label="当前使用次数" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250" fixed="right">
        <template #default="scope">
          <el-button type="primary" link :icon="View" @click="handleView(scope.row)">查看</el-button>
          <el-button type="primary" link :icon="EditPen" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="warning" link :icon="Tools" @click="handleMaintenance(scope.row)">维护</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="toolingFixture">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import { CirclePlus, EditPen, View, Tools } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 查询表单
const searchForm = reactive({
  toolingCode: "",
  toolingName: "",
  toolingType: "",
  status: ""
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取类型颜色
const getTypeColor = (type: string) => {
  const typeMap: Record<string, string> = {
    FIXTURE: "primary",
    MOLD: "success",
    TOOL: "warning",
    GAUGE: "info"
  };
  return typeMap[type] || "info";
};

// 获取类型文本
const getTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    FIXTURE: "夹具",
    MOLD: "模具",
    TOOL: "工具",
    GAUGE: "量具"
  };
  return typeMap[type] || type;
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    AVAILABLE: "success",
    IN_USE: "primary",
    MAINTENANCE: "warning",
    SCRAPPED: "danger"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    AVAILABLE: "可用",
    IN_USE: "使用中",
    MAINTENANCE: "维修中",
    SCRAPPED: "报废"
  };
  return statusMap[status] || status;
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // TODO: 调用API获取工装夹具数据
    console.log("获取工装夹具数据");
  } catch (error) {
    console.error("获取工装夹具列表失败:", error);
    ElMessage.error("获取工装夹具列表失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 新增
const handleAdd = () => {
  ElMessage.info("新增工装夹具功能待实现");
};

// 查看
const handleView = (row: any) => {
  ElMessage.info("查看工装夹具详情功能待实现");
};

// 编辑
const handleEdit = (row: any) => {
  ElMessage.info("编辑工装夹具功能待实现");
};

// 维护
const handleMaintenance = (row: any) => {
  ElMessage.info("工装夹具维护功能待实现");
};

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确认删除工装夹具 "${row.toolingName}" 吗？`, "删除确认", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      ElMessage.success("删除成功");
      getTableData();
    })
    .catch(() => {});
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}
</style>